

package top.continew.admin.question.user.service.question.entity;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.Getter;
import top.continew.admin.question.model.question.Question;
import top.continew.admin.question.user.service.question.enums.ProblemStateEnum;
import top.continew.admin.question.user.service.question.enums.QuestionTypeEnum;

import java.util.ArrayList;
import java.util.List;

public class LinkQuestion extends AbstractQuestion {

    @Getter
    private String lineOptions;

    @Override
    public void init(Question question) {
        super.init(question);
        lineOptions = question.getLineOptions();
    }

    @Override
    public CorrectResult correct(String userAnswer) {
        CorrectResult correctResult = new CorrectResult();
        correctResult.setCorrectAnswer(answer);
        correctResult.setUserAnswer(userAnswer);
        correctResult.setProblemState(ProblemStateEnum.UNCHECK.getCode());
        if (userAnswer != null) {
            // 解析标准答案
            List<LinkAnswer> standardAnswers = JSON.parseArray(answer, LinkAnswer.class);
            // 解析用户答案
            List<JSONObject> userLinks = JSON.parseArray(userAnswer, JSONObject.class);
            List<LinkAnswer> userAnswers = new ArrayList<>();
            for (JSONObject link : userLinks) {
                LinkAnswer linkAnswer = new LinkAnswer();
                linkAnswer.setLeft(link.getInteger("left"));
                linkAnswer.setRight(link.getInteger("right"));
                userAnswers.add(linkAnswer);
            }
            // 比较答案
            if (standardAnswers.size() == userAnswers.size()) {
                boolean allMatch = true;
                for (LinkAnswer standard : standardAnswers) {
                    boolean found = false;
                    for (LinkAnswer user : userAnswers) {
                        if (standard.getLeft().equals(user.getLeft()) && standard.getRight().equals(user.getRight())) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        allMatch = false;
                        break;
                    }
                }
                correctResult.setProblemState(allMatch
                    ? ProblemStateEnum.RIGHT.getCode()
                    : ProblemStateEnum.WRONG.getCode());
            } else {
                correctResult.setProblemState(ProblemStateEnum.WRONG.getCode());
            }
        }
        return correctResult;
    }

    @Getter
    private static class LinkAnswer {
        private Integer left;
        private Integer right;

        public void setLeft(Integer left) {
            this.left = left;
        }

        public void setRight(Integer right) {
            this.right = right;
        }
    }

    @Override
    public Integer getType() {
        return QuestionTypeEnum.LINK_QUESTIONS.getCode();
    }

    @Override
    public String getTypeName() {
        return QuestionTypeEnum.LINK_QUESTIONS.getName();
    }

}